package com.dlsp.hotel.controller.admin.home;

import com.dlsp.hotel.bean.Account;
import com.dlsp.hotel.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:fanxh
 * @date: 2021/12/31/15:09
 * 前台管理
 */
@Controller
@SessionAttributes(names = {"account"})
@RequestMapping("/home/account")

public class HomeAccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 登录页面
     * @return
     */
    @RequestMapping("/loginUi")
    public String loginUi(){
        return  "pages/home/index/login";
    }
    /**
     * 注册页面
     * @return
     */
    @RequestMapping("/registUi")
    public String registUi(){
        return  "pages/home/index/reg";
    }
    /**
     * 客户预定页面
     * @return
     */
    @RequestMapping("/bookOrderUi")
    public String bookOrderUi(){
        return  "pages/home/account/book_order";
    }


    /**
     * 前台个人信息主页
     * @return
     */
    @RequestMapping("/indexUiMyselfCenter")
    public String indexUiMyselfCenter(){
        return  "pages/home/account/index";
    }

    /**
     * 登录
     * @return
     */
    @RequestMapping("/login")
    public String login(String uname, String upwd,String vcode, Model model,HttpServletRequest request ) throws Exception {
        Account account = accountService.login(uname,upwd);
        Object attribute = request.getSession().getAttribute("accountLoginCpacha");
            if (account != null) {
                if(!vcode.equalsIgnoreCase(attribute.toString())){
                    return "redirect:/home/account/loginUi";
                }
                model.addAttribute("account", account);

                return "redirect:/home/index/indexUi";
            }

//
        return "redirect:/home/account/loginUi";
    }
    /**
     * 注册
     * @return
     */
    @RequestMapping(value = "/regist",method=RequestMethod.POST)
    public String regist(Account account,String name){
        if (!isExist(name)){
            accountService.add(account);
            return "redirect:/home/account/loginUi";

        }
//        return "该用户已经存在,请您尝试重新注册";
        return "redirect:/home/account/registUi";

    }

    /**
     * 退出登录
     * @param sessionStatus
     * @return
     */
    @RequestMapping("/logout")
    public String logout(SessionStatus sessionStatus) {
        sessionStatus.setComplete();
        return "redirect:/home/account/loginUi";
    }

    private boolean isExist(String name){
        Account account = accountService.findByName(name);
        if(account == null)return false;
        return true;
    }

}
